package cn.edu.xjtu.android.emoji_warfare_android.engine;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * 多线程同步链表
 * 
 * @author wyyf
 *
 * @param <E>
 *            链表元素类型
 */
public class SyncLinkedList<E> {

    private List<E> list;

    public SyncLinkedList() {
        list = Collections.synchronizedList(new LinkedList<E>());
    }

    public synchronized boolean add(E object) {
        return list.add(object);
    }

    public synchronized boolean addAll(Collection<? extends E> collection) {

        return list.addAll(collection);
    }

    public synchronized boolean remove(E object) {
        return list.remove(object);
    }

    public synchronized E remove(int index) {
        return list.remove(index);
    }

    public synchronized boolean removeAll(Collection<? extends E> collection) {

        return list.removeAll(collection);
    }

    /**
     * 调试用检查元素内容
     */
    @Override
    public synchronized String toString() {
        StringBuilder sb = new StringBuilder();
        for (E e : this.list) {
            sb.append(e.getClass().getName());
            sb.append(";\n");
        }
        return sb.toString();
    }

    public synchronized int size() {
        return list.size();
    }

    public synchronized E get(int index) {
        return list.get(index);
    }
}
